package service

import (
	"singo/model"
	"singo/serializer"
)

// ListUserService 用户列表服务
type ListUserService struct {
	Limit int `form:"pageSize"`
	Start int `form:"pageNo"`
}

// UserList 用户列表
func (service *ListUserService) UserList() serializer.Response {
	users := []model.User{}
	// organizations := []model.Organization{}
	total := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	if err := model.DB.Model(&users).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	// if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Find(&users).Error; err != nil {
	// 	return serializer.Response{
	// 		Status: 50000,
	// 		Msg:    "数据库连接错误",
	// 		Error:  err.Error(),
	// 	}
	// }

	if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Find(&users).Error; err != nil {
		// if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Find(&users).Related(&organizations, "organizations").Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	// for idx, item := range users {
	// 	org := organizations[idx]
	// 	user := model.User{
	// 		UID:      item.UID,
	// 		UserName: item.UserName,
	// 		Nickname: item.Nickname,
	// 		UserType: item.UserType,
	// 		Gender:   item.Gender,
	// 		Intro:    item.Intro,
	// 		Mobile:   item.Mobile,
	// 		Email:    item.Email,
	// 		Status:   item.Status,
	// 		Avatar:   item.Avatar,
	// 		Job:      item.Job,
	// 		Rank:     item.Rank,
	// 		Level:    item.Level,
	// 		// Organization: org,
	// 	}
	// 	fmt.Println(org)
	// 	fmt.Println(user)
	// }
	return serializer.BuildListResponse(serializer.BuildUsers(users), uint(total))
}
